var app = getApp();
var url = app.url;
Page({
  data: {
    alarmNum: 0,

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onShow: function () {
    // 页面显示
    var deptName = wx.getStorageSync('deptName')
    if (deptName === "" || deptName == null) {
      wx.setNavigationBarTitle({
        title: "控制台",
      })
    } else {
      wx.setNavigationBarTitle({
        title: deptName,
      })

    }
    this.checkLogin()
  },
  onReady: function () {
    // 页面渲染完成
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {
    this.setData({ alarmNum: 0 })
    this.checkLogin()
  },
  checkLogin: function () {
    var name = wx.getStorageSync('userName')
    if (name == '' || name == null) {
      wx.showModal({
        title: '提示',
        content: "用户未登录",
        confirmText: '去登录',
        showCancel: true,
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../login/login',
            })
          } else if (res.cancel) {
            wx.navigateTo({
              url: '../index/index',
            })
          }
        }

      })
    } else {
      this.queryAlarm()
    }
  },
  queryAlarm: function () {
    var that = this
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var date = new Date();
    let year = date.getFullYear()
    let mon = date.getMonth() + 1
    let day = date.getDate()
    var dateStr = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day)
    var params = { bdate: dateStr, edate: dateStr }
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: url + '/xywl/appAlarmInfo.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        console.log(rest)
        if (rest.res == 0) {
          var deptId = wx.getStorageSync('deptId')
          var dataList = rest.dataList
          var count = 0;
          for (var i = 0; i < dataList.length; i++) {
            if (dataList[i].PARENTID == deptId || deptId == 1) {
              count++
            }
          }
          that.setData({ alarmNum: count })
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
      }
    })
  },
  bindToQuery: function () {
    wx.navigateTo({
      url: '../control/query',
    })
  },
  bindToReadout: function () {
    wx.navigateTo({
      url: '../control/readout',
    })
  },
  bindToMap: function () {
    wx.navigateTo({
      url: '../map/map',
    })
  },
  bindToKeep: function () {
    wx.navigateTo({
      url: '../control/keepLight',
    })
  },
  bindToMonitor: function () {
    wx.navigateTo({
      url: '../control/monitor',
    })
  },
  bindEXception: function () {
    wx.navigateTo({
      url: '../abnormallyReport/abnormallyReport',
    })
  },
  bindEXceptionList: function () {
    wx.navigateTo({
      url: '../reportList/reportList',
    })
  },
  bindToZdSet: function () {
    wx.navigateTo({
      url: '../control/zdSetup',
    })
  },
});